package com.authine.cloudpivot.ext.utils;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class DateUtils extends org.apache.commons.lang3.time.DateUtils {
    public static final String[] zodiacArr = new String[]{"猴", "鸡", "狗", "猪", "鼠", "牛", "虎", "兔", "龙", "蛇", "马", "羊"};
    public static final String[] constellationArr = new String[]{"水瓶座", "双鱼座", "白羊座", "金牛座", "双子座", "巨蟹座", "狮子座", "处女座", "天秤座", "天蝎座", "射手座", "魔羯座"};
    public static final int[] constellationEdgeDay = new int[]{20, 19, 21, 21, 21, 22, 23, 23, 23, 23, 22, 22};
    public static String MIN_OF_DAY = " 00:00:00";
    public static String MAX_OF_DAY = " 23:59:59";
    public static String[] parsePatterns = new String[]{"yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "HH:mm:ss"};



    public static Date getStartMonthDate(int year, int month) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(year, month - 1, 1);
        return calendar.getTime();
    }

    public static Date getEndMonthDate(int year, int month) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(year, month - 1, 1);
        int day = calendar.getActualMaximum(5);
        calendar.set(year, month - 1, day);
        return calendar.getTime();
    }

    public static Integer getNowYear() {
        Date date = new Date();
        GregorianCalendar gc = (GregorianCalendar)Calendar.getInstance();
        gc.setTime(date);
        return gc.get(1);
    }

    public static int getNowMonth() {
        Date date = new Date();
        GregorianCalendar gc = (GregorianCalendar)Calendar.getInstance();
        gc.setTime(date);
        return gc.get(2) + 1;
    }

    public static int getDiffDays(Date beginDate, Date endDate) {
        if (beginDate != null && endDate != null) {
            long diff = (endDate.getTime() - beginDate.getTime()) / 86400000L;
            int days = (new Long(diff)).intValue();
            return days;
        } else {
            throw new IllegalArgumentException("getDiffDays param is null!");
        }
    }

    public static long dateDiff(Date beginDate, Date endDate) {
        long date1ms = beginDate.getTime();
        long date2ms = endDate.getTime();
        return date2ms - date1ms;
    }

    public static Date max(Date beginDate, Date endDate) {
        if (beginDate == null) {
            return endDate;
        } else if (endDate == null) {
            return beginDate;
        } else {
            return beginDate.after(endDate) ? beginDate : endDate;
        }
    }

    public static Date min(Date beginDate, Date endDate) {
        if (beginDate == null) {
            return endDate;
        } else if (endDate == null) {
            return beginDate;
        } else {
            return beginDate.after(endDate) ? endDate : beginDate;
        }
    }

    public static Date getFirstSeasonDate(Date date) {
        int[] SEASON = new int[]{1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4};
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int sean = SEASON[cal.get(2)];
        cal.set(2, sean * 3 - 3);
        return cal.getTime();
    }

    public static Date getNextDay(Date date, int i) {
        Calendar cal = new GregorianCalendar();
        cal.setTime(date);
        cal.set(5, cal.get(5) + i);
        return cal.getTime();
    }

    public static Date getFrontDay(Date date, int i) {
        Calendar cal = new GregorianCalendar();
        cal.setTime(date);
        cal.set(5, cal.get(5) - i);
        return cal.getTime();
    }

    public static List<List<Date>> getTimeList(int beginYear, int beginMonth, int endYear, int endMonth, int k) {
        List<List<Date>> list = new ArrayList();
        int j;
        if (beginYear == endYear) {
            for(j = beginMonth; j <= endMonth; ++j) {
                list.add(getTimeList(beginYear, j, k));
            }
        } else {
            for(j = beginMonth; j < 12; ++j) {
                list.add(getTimeList(beginYear, j, k));
            }

            for(j = beginYear + 1; j < endYear; ++j) {
                for(int i = 0; i < 12; ++i) {
                    list.add(getTimeList(j, i, k));
                }
            }

            for(j = 0; j <= endMonth; ++j) {
                list.add(getTimeList(endYear, j, k));
            }
        }

        return list;
    }

    public static List<Date> getTimeList(int beginYear, int beginMonth, int k) {
        List<Date> list = new ArrayList();
        Calendar begincal = new GregorianCalendar(beginYear, beginMonth, 1);
        int max = begincal.getActualMaximum(5);

        for(int i = 1; i < max; i += k) {
            list.add(begincal.getTime());
            begincal.add(5, k);
        }

        begincal = new GregorianCalendar(beginYear, beginMonth, max);
        list.add(begincal.getTime());
        return list;
    }

    private static Date format(Date date) {
        SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        try {
            date = sd.parse(sd.format(date));
        } catch (ParseException var3) {
            var3.printStackTrace();
        }

        return date;
    }

    public static String getDate(String pattern) {
        return DateFormatUtils.format(new Date(), pattern);
    }

    public static String getDate(Date date, String pattern) {
        return DateFormatUtils.format(date, pattern);
    }

    public static String getDate() {
        return getDate("yyyy-MM-dd");
    }

    public static String addYear(Date date, Integer number) {
        Calendar rightNow = Calendar.getInstance();
        rightNow.setTime(date);
        rightNow.add(1, number);
        Date dt2 = rightNow.getTime();
        return getDate(dt2, "yyyy-MM-dd");
    }

    public static String addMinute(String date, Integer number) {
        try {
            SimpleDateFormat format1 = new SimpleDateFormat(parsePatterns[1]);
            SimpleDateFormat format2 = new SimpleDateFormat(parsePatterns[2]);
            Date dt1 = format1.parse(date);
            Calendar rightNow = Calendar.getInstance();
            rightNow.setTime(dt1);
            rightNow.add(12, number);
            Date dt2 = rightNow.getTime();
            return format2.format(dt2);
        } catch (ParseException var7) {
            var7.printStackTrace();
            return "";
        }
    }

    public static Date formateDateTime(String dateStr) {
        if (StringUtils.isEmpty(dateStr)) {
            return null;
        } else {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

            try {
                return sdf.parse(dateStr);
            } catch (ParseException var3) {
                return null;
            }
        }
    }

    public static Date formateDate(String dateStr) {
        if (StringUtils.isEmpty(dateStr)) {
            return null;
        } else {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

            try {
                return sdf.parse(dateStr);
            } catch (ParseException var3) {
                return null;
            }
        }
    }

    public static Date formatDate(String dateStr,String pattern) {
        if (StringUtils.isEmpty(dateStr)) {
            return null;
        } else {
            SimpleDateFormat sdf = new SimpleDateFormat(pattern);
            try {
                return sdf.parse(dateStr);
            } catch (ParseException var3) {
                return null;
            }
        }
    }

    public static Date formateDate(long timeInMillis) {
        try {
            return new Date(timeInMillis);
        } catch (Exception var3) {
            return null;
        }
    }

    public static String getCurrentTimeStamp(){
        return new SimpleDateFormat("yyMMddhhmmss").format(System.currentTimeMillis());
    }
}
